package com.android.pantryshopping.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.pantryshopping.R;
import com.android.pantryshopping.bean.Commodity;
import com.android.pantryshopping.bean.ShopList;

import java.util.ArrayList;
import java.util.List;

public class ShopListAdapter extends RecyclerView.Adapter<ShopListAdapter.ViewHolder> {

    private Context mContext;
    private List<ShopList> mShopLists;
    private List<Integer> mCheckDeleteLists;
    // 用来控制CheckBox的选中状况

    private onShopListItemListener mListener;

    public ShopListAdapter(Context context) {
        mContext = context;
        mShopLists = new ArrayList<>();
    }

    public void setShopLists(ArrayList<ShopList> shopListArrayList) {
        mShopLists = shopListArrayList;
    }

    public void setCheckDelete(ArrayList<Integer> checkDeleteList) {
        mCheckDeleteLists = checkDeleteList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.add_shop_list,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ShopList shopList = mShopLists.get(position);
        holder.mTvId.setText(shopList.getId());
        holder.mTvName.setText(shopList.getName());
        holder.mTvPrice.setText(String.valueOf(shopList.getCommodityList()));

        holder.itemView.setOnClickListener(v -> {
            mListener.onShopClick(position);
        });

        holder.mCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mListener.onCheckShopDelete(position);
            }else {
                mListener.onRemoveShopCheckDelete(position);
            }
        });

        if (mCheckDeleteLists.contains(position)) {
            holder.mCheckBox.setChecked(true);
        }else {
            holder.mCheckBox.setChecked(false);
        }
    }

    @Override
    public int getItemCount() {
        return mShopLists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mTvId;
        private TextView mTvName;
        private TextView mTvPrice;
        private CheckBox mCheckBox;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            mTvId = itemView.findViewById(R.id.tv_id);
            mTvName = itemView.findViewById(R.id.tv_name);
            mTvPrice = itemView.findViewById(R.id.tv_price);
            mCheckBox = itemView.findViewById(R.id.chk_delete);
        }
    }

    public interface onShopListItemListener{
        void onShopClick(int position);

        void onCheckShopDelete(int position);

        void onRemoveShopCheckDelete(int position);
    }

    public void setOnCommodityItemListener(onShopListItemListener listener) {
        mListener = listener;
    }
}
